//var eDatMain;
function endWith(s1, s2){
    return s1.match(s2+"$") == s2
}

function startWidth(s1, s2) {
    return s1.match("^"+s2) == s2
}

function trim(s)
{
    return s.replace(/(^\s+)|(\s+$)/g, "");
}


// whitespace characters
var whitespace = " \t\n\r";
var blurId = 'overlay'
var transparentId = 'overlayTransparent'

/****************************************************************/

// Check whether string s is empty.
function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}

/****************************************************************/

function isWhitespace (s)
{
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


function openHelpWindow(url) {
    var targetWindow = window.opener.openHelpPageWindow(helpServerPrefix + url, "HELP");

    if (targetWindow) targetWindow.focus();
    return false;
}


function openNoPrefixHelpWindow(url) {
    var targetWindow = window.opener.openHelpPageWindow(url, "HELP");

    if (targetWindow) targetWindow.focus();
    return false;
}


function setInitialFocus(id) {
    document.getElementById(id)?document.getElementById(id).focus():null;
}

var isFromAlertWindow;
var isTextField;

function handleFormEnterKey(eventObj, obj) {
    if ( isTextField ) {
        if (eventObj && eventObj.which) {
            if (isFromAlertWindow != true) {
                (eventObj.which == 13)?document.getElementById(obj).click():null;
            }
            else {
                isFromAlertWindow = false;
            }
        }
        else {
            (eventObj.keyCode == 13)?document.getElementById(obj).click():null;
        }
    }
    return false;
}

function swapVisibility(id1, id2) {
    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj1Parent = obj1.parentNode;
    obj1Parent.removeChild(obj1);
    obj1Parent.appendChild(obj2);
}

var hiddenObjectNotAppendedFlags = new Object();
function switchToHiddenButton (id1, id2) {
    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj1Parent = obj1.parentNode;
    obj1.style.display='none';

    obj2Clone = obj2.cloneNode(true);
    // TODO: uncomment
    //  if (!hiddenObjectNotAppendedFlags[id2]) {
    obj1Parent.appendChild(obj2Clone);
//      hiddenObjectNotAppendedFlags[id2] = obj2Clone;
//  }
}

function switchToDefaultButton (id1, id2) {
    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj1Parent = obj1.parentNode;

    obj1.style.display='inline';
    obj1Parent.removeChild(obj2);
}

function showSearchMessage() {
    var obj = document.getElementById('searchInProgressMessage');
    obj.style.position = 'relative';
    obj.style.visibility = 'visible';
}

function hideSearchMessage() {
    var obj = document.getElementById('searchInProgressMessage');
    obj.style.position = 'absolute';
    obj.style.visibility = 'hidden';
}

function lineupTables(tblHeaderID, tblContentID) {
    if (document.getElementById(tblHeaderID) == null || document.getElementById(tblContentID) == null) return;
    var tblHeader = document.getElementById(tblHeaderID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    var tblHeaderChildsColumn = tblHeader.getElementsByTagName("tr")[0].getElementsByTagName("th");
    var tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    var tblContentChildsColumn = tblContent.getElementsByTagName("tr")[0].childNodes;
    for (var i = 0; i < tblHeaderChildsColumn.length; i++ ) {
        var tdHeader = tblHeaderChildsColumn.item(i);
        var tdContent = tblContentChildsColumn.item(i);
        var width = tdHeader.offsetWidth > tdContent.offsetWidth ? tdHeader.offsetWidth : tdContent.offsetWidth;
        tdHeader.width = width;
        tdContent.width = width;
    }
}

function lineupSelectManyTables(tblHeaderID, tblContentID) {
    if (document.getElementById(tblHeaderID) == null || document.getElementById(tblContentID) == null) return;
    var tblHeader = document.getElementById(tblHeaderID).getElementsByTagName("table")[1];
    var tblHeaderChildsColumn = tblHeader.getElementsByTagName("tr")[0].getElementsByTagName("th");
    var tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    var tblContentChildsColumn = tblContent.getElementsByTagName("tr")[0].getElementsByTagName("td");

    if (tblHeaderChildsColumn.length != tblContentChildsColumn.length) {
        tblContent.getElementsByTagName("tr")[0].style.display = 'none';
        tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[1];
        tblContentChildsColumn = tblContent.getElementsByTagName("tr")[0].getElementsByTagName("td");
    }
    if (tblHeaderChildsColumn.length != tblContentChildsColumn.length) {
        return;
    }

    for (var i = 0; i < tblHeaderChildsColumn.length; i++ ) {
        var tdHeader = tblHeaderChildsColumn.item(i);
        var tdContent = tblContentChildsColumn.item(i);
        var width = tdHeader.offsetWidth > tdContent.offsetWidth ? tdHeader.offsetWidth : tdContent.offsetWidth;
        tdHeader.width = width;
        tdContent.width = width;
    }
}

function setDefaultRadiobutton(tmpObj) {
    var tmpResult = false;
    if (tmpObj != null) {
        for (var i = 0; i < tmpObj.length; i++) {
            if (tmpObj[i].checked == true) {
                tmpResult = true;
                break;
            }
        }

        if (!tmpResult && tmpObj.length > 0) {
            tmpObj[0].checked = true;
        }
    }
}

function disableElement(element) {
    element.readOnly='true';
    if (document.all) {
        /*only for IE*/
        element.blur();
    }
}

function hideSelectOneTableHeader(tblHeaderID, tblContentID) {
    if (document.getElementById(tblHeaderID) == null || document.getElementById(tblContentID) == null) return;
    var tblHeader = document.getElementById(tblHeaderID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    tblHeader.getElementsByTagName("tr")[1].style.display = 'none';
    var tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    var tblContentChildColumns = tblContent.getElementsByTagName("tr")[0].getElementsByTagName("th");
    for (var i = 0; i < tblContentChildColumns.length; i++ ) {
        var tdContent = tblContentChildColumns.item(i);
        var imgWidth;
        if (tdContent.width.indexOf('%') == -1) {
            imgWidth = tdContent.width;
        } else {
            imgWidth = tdContent.offsetWidth - 3;
        }
        var newHTML = "<img border='0' src='image/1x1_transparent.png' width='" + imgWidth + "' height='1'/>";
        tdContent.innerHTML = newHTML;
    }
    cleanSelectOneTableCheckedSlectorUpOnClickAction(tblContentID);
}

// ancillary routine for clean up onClick action from checked radio buttons (SelectOne tables)
function cleanSelectOneTableCheckedSlectorUpOnClickAction(tblContentID) {
    if (document.getElementById(tblContentID) == null) return;
    var selectors = document.getElementById(tblContentID).getElementsByTagName("input");
    for (var i = 0; i < selectors.length; i++) {
        var selector = selectors[i];
        if (selector.type != "radio") continue;
        if (selector.checked) {
            selector.onclick = "";
            break;
        }
    }
}

function hideSelectManyTableHeader(tblHeaderID, tblContentID, tblContentResultRowCount) {
    if (document.getElementById(tblHeaderID) == null || document.getElementById(tblContentID) == null) return;
    var tblHeader = document.getElementById(tblHeaderID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    tblHeader.getElementsByTagName("tr")[1].style.display = 'none';
    var tblContent;
    if (tblContentResultRowCount == 0) {
        tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[0];
    } else {
        var tblContentBase = document.getElementById(tblContentID).getElementsByTagName("table")[0];
        tblContent = document.getElementById(tblContentID).getElementsByTagName("table")[0].getElementsByTagName("table")[1];
        tblContentBase.getElementsByTagName("tr")[0].style.display = 'none';
    }
    var tblContentChildColumns = tblContent.getElementsByTagName("tr")[0].getElementsByTagName("th");
    for (var i = 0; i < tblContentChildColumns.length; i++ ) {
        var tdContent = tblContentChildColumns.item(i);
        var imgWidth;
        if (tdContent.width.indexOf('%') == -1) {
            imgWidth = tdContent.width;
        } else {
            imgWidth = tdContent.offsetWidth - 3;
        }
        var newHTML = "<img border='0' src='image/1x1_transparent.png' width='" + imgWidth + "' height='1'/>";
        tdContent.innerHTML = newHTML;
    }
}

function highlightBGLightGreenFor286History(tableId, columnForInspection, displayedRecords) {
    if (document.getElementById(tableId) == null || displayedRecords == 0) return;
    var rows = document.getElementById(tableId).getElementsByTagName("table")[0].getElementsByTagName("table")[0].getElementsByTagName("tr");
    var text;
    var cells;
    for (var i = 1; i < rows.length; i++) {
        cells = rows[i].getElementsByTagName("td");
        text = cells[columnForInspection].innerHTML;
        if (text == "" || text == " " || text == "EPIC MODEL NOT TABBED") {
            for (var j = 0; j < cells.length; j++) {
                cells[j].style.backgroundColor = "#CCFFCC";
            }
        }
    }
}

function getObjectPosition(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function cancelEvent() {
    try {
        window.stop();
    } catch (exp) {
        document.execCommand('Stop');
    }
    hideOverlay();
    window.status = 'Done';

    try {
        initialAction();
    } catch (exp) {
    }
}

function setStyle(el, styleText) {
    if (el.style.setAttribute) {
        el.style.setAttribute("cssText", styleText);
    } else {
        el.setAttribute("style", styleText);
    }
}

function showOverlay(bCancel, docRoot, transparent) {

    var documentRoot;
    if (docRoot === undefined) {
        documentRoot = document;
    } else {
        documentRoot = docRoot.document;
    }

    var isTransparent = transparent != undefined && transparent;
    var overlayId = isTransparent ? transparentId : blurId

    var objOverlay = documentRoot.getElementById(overlayId);
    var bNewCreated = false;

    if (objOverlay == null) {
        objOverlay = documentRoot.createElement("div");
        bNewCreated = true;
    }

    var cursorStyle = '';
    if (bCancel && !isTransparent) {
        objOverlay.setAttribute('id', overlayId);
        cursorStyle = 'cursor: pointer;';
    } else {
        objOverlay.setAttribute('id', overlayId);
        cursorStyle = 'cursor: wait;';
    }

    var arrayPageSize = getPageSize(docRoot);
    setStyle(objOverlay, 'width: ' + arrayPageSize[0] + 'px; height: ' + arrayPageSize[1] + 'px; display: block;' + cursorStyle);

    if (isIE6()) {
        // for IE6 only: hide all 'select' elements
        var selectItems = documentRoot.forms[0].getElementsByTagName("select");
        for (var i = 0; i < selectItems.length; i++) {
            selectItems[i].style.display = 'none';
        }
    }

    if (!bNewCreated) {
        return;
    }

    if(!isTransparent) {
        var str = '';
        if (bCancel) {
            if (objOverlay.addEventListener){
                objOverlay.addEventListener('click', cancelEvent, false);
            } else if (objOverlay.attachEvent){
                // IE
                objOverlay.attachEvent('onclick', cancelEvent);
            }

            str = '<span style="position: absolute; top: ' + (arrayPageSize[3] / 2 + 40) + 'px; left: ' + (arrayPageSize[2] / 2 - 80) + 'px;">Click anywhere to cancel</span>';
        } else {
            str = '<span style="position: absolute; top: ' + (arrayPageSize[3] / 2 + 40) + 'px; left: ' + (arrayPageSize[2] / 2 - 40) + 'px;">Please wait</span>';
        }
        str += '<img src="../image/progress.gif" style="position: absolute; top: ' + (arrayPageSize[3] / 2 - 33) + 'px; left: ' + (arrayPageSize[2] / 2 - 30) + 'px;"/>';

        objOverlay.innerHTML = str;
    }
    documentRoot.body.appendChild(objOverlay);
    try {
        objOverlay.focus();
    } catch (exp) {
    }
}

function hideOverlay() {
    var objOverlay = document.getElementById(blurId);

    if (objOverlay != null) {
        document.body.removeChild(objOverlay);
    }

    objOverlay = document.getElementById(transparentId);

    if (objOverlay != null) {
        document.body.removeChild(objOverlay);
    }

    if (isIE6()) {
        var selectItems = document.forms[0].getElementsByTagName("select");
        for (var i = 0; i < selectItems.length; i++) {
            selectItems[i].style.display = 'block';
        }
    }

    window.status = 'Done';
}

function getPageSize(docRoot) {
    var xScroll, yScroll;

    var windowRoot;
    var documentRoot;
    if (docRoot === undefined) {
        windowRoot = window;
        documentRoot = document;
    } else {
        windowRoot = docRoot;
        documentRoot = docRoot.document;
    }

    if (windowRoot.innerHeight && windowRoot.scrollMaxY) {
        xScroll = documentRoot.body.scrollWidth;
        yScroll = windowRoot.innerHeight + windowRoot.scrollMaxY;
    } else if (documentRoot.body.scrollHeight > documentRoot.body.offsetHeight) {
        xScroll = documentRoot.body.scrollWidth;
        yScroll = documentRoot.body.scrollHeight;
    } else {
        xScroll = documentRoot.body.offsetWidth;
        yScroll = documentRoot.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (windowRoot.innerHeight) {
        windowWidth = windowRoot.innerWidth;
        windowHeight = windowRoot.innerHeight;
    } else if (documentRoot.documentElement && documentRoot.documentElement.clientHeight) {
        windowWidth = documentRoot.documentElement.clientWidth;
        windowHeight = documentRoot.documentElement.clientHeight;
    } else if (documentRoot.body) {
        windowWidth = documentRoot.body.clientWidth;
        windowHeight = documentRoot.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}

function cancelAction(idsButtonsToEnable, idsButtonsToDisable) {
    try {
        window.stop();
    } catch (exp) {
        document.execCommand('Stop');
    }
    for (var i = 0; i < idsButtonsToEnable.length; i++) {
        switchToDefaultButton(idsButtonsToEnable[i], idsButtonsToEnable[i] + 'Disabled');
    }
    for (var i = 0; i < idsButtonsToDisable.length; i++) {
        switchToDefaultButton(idsButtonsToDisable[i] + 'Disabled', idsButtonsToDisable[i]);
    }
    hideSearchMessage();
    hiddenObjectNotAppendedFlags = new Object();
}

function submitFormByEnter(event, formId, buttonId, parentForBlock) {
    var code = (window.event) ? window.event.keyCode : event.keyCode;
    if (code == 13) {
        var showOverlayFlag = true;
        if (parentForBlock !== undefined) {
            showOverlayFlag = false;
            showOverlay(false, parentForBlock, true)
            showOverlay(false, undefined, true)

        } else {
            showOverlay(showOverlayFlag);
        }

        submitForm(formId, 1, {
            source: buttonId
        });
    }
}

function isIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}

function cleanSelectorUpOnClickAction(tblContentID) {
    var element = document.getElementById(tblContentID);
    if (element == null) return;

    if (element.tagName == "INPUT" && element.type == "radio") {
        if (element.checked) {
            element.onclick = "";
            element.parentNode.onclick = "";
        }
    } else {

        var selectors = element.getElementsByTagName("input");
        for (var i = 0; i < selectors.length; i++) {
            var selector = selectors[i];
            if (selector.type != "radio") continue;
            if (selector.checked) {
                selector.onclick = "";
                selector.parentNode.onclick = "";
                break;
            }
        }
    }
}

function openHelpWND(url) {
    var targetWindowName = "HELP";
    var wnd = window.open(url, targetWindowName, "menubar=1,resizable=1,scrollbars=1,status=yes,width=1280,height=1024");
    wnd.focus();
}


function hasSelectedTableRow(tableId){
    var theTBody = document.getElementById(tableId).getElementsByTagName("table")[0].getElementsByTagName("table")[1].tBodies[0];
    var rows = theTBody.getElementsByTagName("tr");

    for (i = 1; i < rows.length; i++)
        if(rows[i].firstChild.firstChild.checked) {
            return true
        }
    return false;
}

// function to allow the moving of the window location by link
// to center
function move_2_center()
{
    setTimeout("_move_2_center()", 1000);
}

function _move_2_center()
{
    try {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            //        myWidth = window.innerWidth+getScrollerWidth();
            myWidth = window.innerWidth+10;
            //            myHeight = window.innerHeight+90; //60 is the total size of title bar and status bar;
            myHeight = window.document.height+90; //60 is the total size of title bar and status bar;
            if(myWidth>screen.availWidth){
                myWidth=screen.availWidth;
            }
            if(myHeight>screen.availHeight){
                myHeight=screen.availHeight;
            }
            try{
                window.top.resizeTo(myWidth, myHeight);
            }catch(except){
                move_2_center();
            }

        } else if( document.documentElement &&
            ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            //            alert(document.documentElement.clientWidth);
            myWidth = document.documentElement.clientWidth+29;
            //            myWidth = document.body.offsetWidth;
            myHeight=document.body.clientHeight+90;//60 is the total size of title bar and status bar
            //        myHeight=document.documentElement.clientHeight;//60 is the total size of title bar and status bar
            if(myWidth>screen.availWidth){
                myWidth=screen.availWidth;
            }
            if(myHeight>screen.availHeight){
                myHeight=screen.availHeight;
            }
            try{
                window.top.resizeTo(myWidth, myHeight);

            }catch(except){
                move_2_center();
            }
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth+29;
            myHeight = document.body.clientHeight+90; //60 is the total size of title bar and status bar;
            if(myWidth>screen.availWidth){
                myWidth=screen.availWidth;
            }
            if(myHeight>screen.availHeight){
                myHeight=screen.availHeight;
            }
        //            try{
        //                window.top.resizeTo(myWidth, myHeight);
        //
        //            }catch(except){
        //                move_2_center();
        //            //alert(except);
        //            }
        }
        var x = (screen.availWidth-myWidth)/2;
        var y = (screen.availHeight-myHeight)/2;
        top.moveTo(x,y<0?0:y);
    }catch (e) {
        move_2_center();
    }
}



function getUrlVars(url)
{
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


